/*
 * =====================================================================================
 *
 *       Filename:  Http2Client.cc
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  11/14/19 14:51:18
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  XXXXXXX (), XXXX@jd.com
 *        Company:  JDAI
 *        Copyright:  Copyright 2019, XXXXXXX
 *
 * =====================================================================================
 */

#include "Http2Client.h"

#include <iostream>
using std::cout;
using std::endl;

bool Http2Client::gmIsGlobalInit = false;
static const string TAG = "Http2Client";

static int RecvDataCB(void* buf, size_t size, size_t nmemb, void* usrCtx) {
    int total = size * nmemb;
    Http2Client* ctx = (Http2Client*) usrCtx;
        

    //通过回调给到用户层
    if (ctx->mpListener != NULL) {
        ctx->mpListener->onRecvData(ctx->mpUsrCtx, buf, total);
    }

    return 0;
}

static size_t dumpHeaderCB(void* ptr, size_t size, size_t nmemb, void* data) {
    printf("recv head=%s\n", (char*)ptr);

    return (size * nmemb);

}

Http2Client::Http2Client(HttpListener* listener) {

    mpListener = listener;

    if (gmIsGlobalInit == false) {
        curl_global_init(CURL_GLOBAL_ALL);  //为防止多线程初始化,将在主线程创建时初始化
        gmIsGlobalInit = true;
    }

    mpHeadList = NULL;

    mpCurl = curl_easy_init();

    if (mpCurl) {
        curl_easy_setopt(mpCurl, CURLOPT_NOSIGNAL, 1L);
        curl_easy_setopt(mpCurl, CURLOPT_CONNECTTIMEOUT, 10);
        curl_easy_setopt(mpCurl, CURLOPT_TIMEOUT, 20);

        curl_easy_setopt(mpCurl, CURLOPT_HEADERFUNCTION, dumpHeaderCB);
        //curl_easy_setopt (mpCurl, CURLOPT_ERRORBUFFER, mpCurlErrBuf);
        curl_easy_setopt(mpCurl, CURLOPT_WRITEFUNCTION, RecvDataCB);
        curl_easy_setopt(mpCurl, CURLOPT_WRITEDATA, this);
        //curl_easy_setopt (mpCurl, CURLOPT_VERBOSE, 0L);    //发布时，设为０
        curl_easy_setopt(mpCurl, CURLOPT_VERBOSE, 1L);  //发布时，设为０

#if 0
        curl_easy_setopt(mpCurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);  //http2 must
        curl_easy_setopt(mpCurl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(mpCurl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
    }

}
    

Http2Client::~Http2Client() {
    if (mpCurl) {
        curl_easy_cleanup(mpCurl);
        mpCurl = NULL;
    }

    if (mpHeadList) {
        curl_slist_free_all(mpHeadList);
        mpHeadList = NULL;
    }

}

void Http2Client::setSrvURL(const string& url) {
    curl_easy_setopt(mpCurl, CURLOPT_URL, url.c_str());
}

int Http2Client::addHead(const string headList[], int len) {
    int i = 0;

    for (i = 0; i < len; i++) {
        cout << "TAG=" << __FILE__ << ", head=" << headList[i].c_str() << endl;
        mpHeadList = curl_slist_append(mpHeadList, headList[i].c_str());
    }

    curl_easy_setopt(mpCurl, CURLOPT_HTTPHEADER, mpHeadList);

    return 0;
}

void Http2Client::freeHead() {
    if (mpHeadList) {
        curl_slist_free_all(mpHeadList);
        mpHeadList = NULL;
    }
}

int Http2Client::post(const string& data) {
    cout <<"F:" << __FILE__ << ", f:" << __func__ << endl;
    CURLcode res;
    string ret = "test";

    curl_easy_setopt(mpCurl, CURLOPT_POST, 1);
    curl_easy_setopt(mpCurl, CURLOPT_POSTFIELDS, data.c_str());
    curl_easy_setopt(mpCurl, CURLOPT_POSTFIELDSIZE, data.size());
    res = curl_easy_perform(mpCurl);

    if (res != CURLE_OK) {
        cout <<"F:" << __FILE__ << ", f:" << __func__ <<", curl err=" << res << endl;
        return -1;
    }

    return 0;
}
